package com.st.event.demo.proxy.factory;

import com.st.event.demo.post.OrderFinishTriggerListener;
import com.st.event.demo.source.EventAttributeSource;
import com.st.event.demo.strategy.EventStrategy;

import java.lang.reflect.Proxy;
import java.util.Map;

public class EventProxyFactory<T> {

    private final Class<T> clientInterface;

    public EventProxyFactory(Class<T> clientInterface) {
        this.clientInterface = clientInterface;
    }

    @SuppressWarnings("unchecked")
    protected T newInstance(EventClientProxy<T> eventClientProxy) {
        return (T) Proxy.newProxyInstance(clientInterface.getClassLoader(), new Class[]{clientInterface}, eventClientProxy);
    }

    public T newInstance(EventAttributeSource eventAttributeSource, EventStrategy eventStrategy, Map<String, OrderFinishTriggerListener> listenerMap) {
        EventClientProxy<T> clientProxy = new EventClientProxy<>(this.clientInterface, eventAttributeSource, eventStrategy, listenerMap);
        return newInstance(clientProxy);
    }
}
